package com.shigu.account.service;

import com.gexin.fastjson.JSON;
import com.opentae.data.mall.beans.MallBonus;
import com.opentae.data.mall.beans.UserInviteInfo;
import com.opentae.data.mall.beans.VipBonus;
import com.opentae.data.mall.interfaces.MallBonusMapper;
import com.opentae.data.mall.interfaces.MallWalletMapper;
import com.opentae.data.mall.interfaces.UserInviteInfoMapper;
import com.opentae.data.mall.interfaces.VipBonusMapper;
import com.shigu.account.api.MallBonusService;
import com.shigu.account.enums.MoneyRecordType;
import com.shigu.account.enums.VipLevel;
import com.shigu.account.exception.UtilsAccountException;
import com.shigu.constant.enums.PageUri;
import com.shigu.constant.enums.VipConstant;
import com.shigu.constant.enums.VipSetting;
import com.shigu.constant.service.SystemConstantService;
import com.shigu.util.MoneyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/*
 * create by  过儿  2019-06-17
 *
 */
@Service(value = "mallBonusServiceImpl")
public class MallBonusServiceImpl implements MallBonusService {

    @Autowired
    MallBonusMapper mallBonusMapper;

    @Autowired
    UserAccountService userAccountService;
    @Autowired
    UserInviteInfoMapper userInviteInfoMapper;
    @Autowired
    SystemConstantService systemConstantService;
    @Autowired
    MallWalletMapper mallWalletMapper;
    @Autowired
    VipBonusMapper vipBonusMapper;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public void doDistributeReward(String tradeCode, Long rewardId) throws UtilsAccountException {
        if (rewardId == null) {
            return;
        }
        //sql 里查询的是未做过分配的
        MallBonus bonus = mallBonusMapper.selTradeBonus(tradeCode);
        if (bonus == null) {
            return;
        }
        //查询邀请人的抽成
        Map<String, String> map = systemConstantService.selPageConstant(PageUri.vip.toString());
        String per = map.get(VipSetting.inviteProfit.toString());// 后台设置的是整数
        Double relPercent = MoneyUtil.divide(per, "100");

        //对各个等级进行分润
        if (bonus.getStaff() != null) {
            userAccountService.addMoney(bonus.getStaff(), bonus.getStaffBonus(), MoneyRecordType.reward.getText());
            mallWalletMapper.changeTotal(bonus.getStaff(), bonus.getStaffBonus());
            choucheng(bonus.getStaff(), bonus.getStaffBonus(), relPercent);
        }

        if (bonus.getOwner() != null) {
            String remark = VipLevel.OWNER.getTitle() + MoneyRecordType.profit.getText();
            //如果分享人等级就是店长
            if (rewardId.equals(bonus.getOwner())) {
                remark = MoneyRecordType.reward.getText();
            }
            userAccountService.addMoney(bonus.getOwner(), bonus.getOwnerBonus(), remark);
            mallWalletMapper.changeTotal(bonus.getOwner(), bonus.getOwnerBonus());
            choucheng(bonus.getOwner(), bonus.getOwnerBonus(), relPercent);
        }

        if (bonus.getBoss() != null) {
            String remark = VipLevel.BOSS.getTitle() + MoneyRecordType.profit.getText();
            //如果分享人等级就是boss
            if (rewardId.equals(bonus.getBoss())) {
                remark = MoneyRecordType.reward.getText();
            }
            userAccountService.addMoney(bonus.getBoss(), bonus.getBossBonus(), remark);
            mallWalletMapper.changeTotal(bonus.getBoss(), bonus.getBossBonus());
            choucheng(bonus.getBoss(), bonus.getBossBonus(), relPercent);
        }
        //邀请人的分享数量增加
        userInviteInfoMapper.changeShareNum(rewardId, bonus.getNum());

        bonus.setStatus(true);//置为已操作
        bonus.setDealTime(new Date());
        mallBonusMapper.updateByPrimaryKeySelective(bonus);
    }

    /**
     * @description 抽成
     * @author 郭尔
     * @date 2019-06-21 15:12
     */
    @Transactional(rollbackFor = {Exception.class})
    public void choucheng(Long userId, Double money, Double relPercent) throws UtilsAccountException {
        if (userId == null) {
            return;
        }
        Long agentId = userInviteInfoMapper.getAgent(userId);
        if (agentId == null) {
            return;
        }

        //计算抽成, 基数*比例 然后格式化
        double choucheng = MoneyUtil.formatMoneyDouble(MoneyUtil.multipy(money, relPercent));
        // 大于0 才进行计算
        if (choucheng > 0) {
            userAccountService.reduceMoney(userId, choucheng, MoneyRecordType.inviteReduce.getText());
            mallWalletMapper.changeTotal(userId, -choucheng);

            userAccountService.addMoney(agentId, choucheng, MoneyRecordType.inviteAdd.getText());
            mallWalletMapper.changeTotal(agentId, choucheng);
        }
    }


    @Override
    public void doVipBonus(Long recordId) throws UtilsAccountException {
        VipBonus bonus = vipBonusMapper.selectByPrimaryKey(recordId);
        if (bonus != null && !bonus.getStatus()) {
            Map<String, String> map = systemConstantService.selPageConstant(PageUri.vip.toString());

            if (bonus.getOwner() != null) {
                Double ownerAmount = Double.valueOf(map.get(VipSetting.ownerAmount.toString()));
                //给店长加钱
                userAccountService.addMoney(bonus.getOwner(), ownerAmount, MoneyRecordType.ownerAmount.getText());
                mallWalletMapper.changeTotal(bonus.getOwner(), ownerAmount);
                bonus.setOwnerAmonut(ownerAmount);
                //店长同级奖励的处理
                Long ownerAgentId = userInviteInfoMapper.getAgent(bonus.getOwner());
                UserInviteInfo ownerAgent = userInviteInfoMapper.selectByPrimaryKey(ownerAgentId);
                //邀请人同为店长
                if( ownerAgent != null && ownerAgent.getVipLevel() == VipLevel.OWNER.getValue()){

                    Double ownerSameLvBonus = Double.valueOf( map.get(VipSetting.ownerSameLvBonus.toString()) );
                    userAccountService.addMoney(ownerAgentId,ownerSameLvBonus,MoneyRecordType.ownerSameLvBonus.getText());
                    mallWalletMapper.changeTotal(ownerAgentId,ownerSameLvBonus);
                    bonus.setSameLvOwner(ownerAgentId);
                    bonus.setLvOwnerAmount(ownerSameLvBonus);
                }
            }

            if (bonus.getBoss() != null) {
                Double bossAmount = Double.valueOf(map.get(VipSetting.bossAmount.toString()));
                //给boss加钱
                userAccountService.addMoney(bonus.getBoss(), bossAmount, MoneyRecordType.bossAmount.getText());
                mallWalletMapper.changeTotal(bonus.getBoss(), bossAmount);
                bonus.setBossAmount(bossAmount);
                //boss同级奖励的处理
                Long bossAgentId = userInviteInfoMapper.getAgent(bonus.getBoss());
                UserInviteInfo bossAgent = userInviteInfoMapper.selectByPrimaryKey(bossAgentId);
                //邀请人同为boss
                if (bossAgent != null && bossAgent.getVipLevel() == VipLevel.BOSS.getValue()) {
                    Double bossSameLvBonus = Double.valueOf(map.get(VipSetting.bossSameLvBonus.toString()));
                    userAccountService.addMoney(bossAgentId, bossSameLvBonus, MoneyRecordType.bossSameLvBonus.getText());
                    mallWalletMapper.changeTotal(bossAgentId, bossSameLvBonus);
                    bonus.setSameLvBoss(bossAgentId);
                    bonus.setLvBoosAmount(bossSameLvBonus);
                }
            }

            bonus.setStatus(true);
            bonus.setDealTime(new Date());
            vipBonusMapper.updateByPrimaryKeySelective(bonus);
        }
    }

}
